package com.ruoyi.project.index;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.order.domain.BinsOrder;
import com.ruoyi.project.order.domain.BinsOrderStt;
import com.ruoyi.project.order.service.IBinsOrderService;
import com.ruoyi.project.order.service.IOrderStatService;
import com.ruoyi.project.order.service.date.DateUtil;
import com.ruoyi.project.user.service.IUserInfoService;
import com.ruoyi.project.vehicle.service.IBinsVehicleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title : IndexController
 * @Author : Administrator
 * @Date : 2025/4/18
 * @Version : 1.0.0
 * @Description :
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IBinsOrderService binsOrderService;
    @Autowired
    private IBinsVehicleInfoService binsVehicleInfoService;
    @Autowired
    private IUserInfoService userInfoService;
    /**
     * 获取首页卡片信息
     * @return
     */
    @RequestMapping("/getCard")
    public AjaxResult getCard() {
        List<BinsOrder> orderList = binsOrderService.selectBinsOrderList(null);
        int orderCount = 0;
        if (orderList != null){
            orderCount = orderList.size();
        }
        List vehicleList = binsVehicleInfoService.selectBinsVehicleInfoList(null);
        int vehicleCount = 0;
        if (vehicleList != null ){
            vehicleCount = vehicleList.size();
        }

        List userList = userInfoService.selectUserInfoList(null);
        int userCount = 0;
        if (userList!= null ){
            userCount = userList.size();
        }

        BinsOrder binsOrder = new BinsOrder();
        binsOrder.setOrderStatus(3);
        List<BinsOrder> binsOrderList= binsOrderService.selectBinsOrderList(binsOrder);
        BigDecimal orderAmount = BigDecimal.ZERO;
        if (binsOrderList!= null){
            for (BinsOrder binsOrderInfo: binsOrderList) {
                orderAmount = orderAmount.add(binsOrderInfo.getOrderPrice());
            }
        }

        Map map = new HashMap();
        map.put("userCount", userCount);
        map.put("carCount", vehicleCount);
        map.put("orderCount", orderCount);
        map.put("orderAmount", orderAmount.setScale(2));
        return AjaxResult.success(map);
    }

    @Autowired
    private IOrderStatService orderStatService;

    @GetMapping("/getLine")
    public AjaxResult getLine() {
        BinsOrderStt binsOrderStt = new BinsOrderStt();
        binsOrderStt.setStartDate(DateUtil.getAppointDate(DateUtil.getEasyDate(), -7, "yyyy-MM-dd"));
        binsOrderStt.setEndDate(DateUtils.getDate());
        List<Map> list = orderStatService.getOrderStatByMarch(binsOrderStt);
        List orderCountList = new ArrayList<>();
        List orderAmountList = new ArrayList<>();
        List dateList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            orderCountList.add(map.get("orderCount"));
            orderAmountList.add(map.get("totalAmount"));
            dateList.add(map.get("payDate"));
        }
        Map map = new HashMap();
        map.put("orderCountList", orderCountList);
        map.put("orderAmountList", orderAmountList);
        map.put("dateList", dateList);
        map.put("expectedData", orderCountList);
        map.put("actualData", orderAmountList);
        map.put("lineKey", dateList);
        return AjaxResult.success(map);
    }

    /**
     * 获取车辆地图信息（热力图）
     * @return
     */
    @RequestMapping("/getVehicleHeadMap")
    public AjaxResult getVehicleHeadMap(Long deptId){
        List<Map> list = binsVehicleInfoService.getVehicleHeadMap(deptId);
        return AjaxResult.success(list);
    }
}
